'use strict';

module.exports = appInfo => {
  const config = exports = {};

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1515985062841_5009';

  // add your config here
  config.middleware = ['notfoundHandler','auth','filter'];

  // 跨域设置
  exports.cors = {
    origin: '*',
    credentials: true,
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  };
  // 安全规则配置
  exports.security = {
    xframe: {
      enable: false
    },
    csrf: {
      enable: false,
      ignoreJSON: true // 默认为 false，当设置为 true 时，将会放过所有 content-type 为 `application/json` 的请求
    }
  };


  // Session 配置
  exports.session = {
    key: 'EGG_SESS',
    maxAge: 1000, // 1 天
    httpOnly: true,
    encrypt: true,
  };

  // 静态文件夹配置
  exports.static = {
    prefix: '/public/',
    dynamic: true,
    maxAge: 0
  };

  // exports.notfound = {
  //   pageUrl: '/404'
  // };

  // 配置错误
  exports.onerror = {
    html(err, ctx) {
      // ctx.redirect('/500');

      ctx.body = `<h3>错误信息：${err.message}</h3>`;
      ctx.status = 200;
    },
    json(err, ctx) {
      ctx.body = {
        succcess: false,
        msg: err.message
      };
      ctx.status = 200;
    }
  };


  // 模版引擎配置
  exports.view = {
    defaultViewEngine: 'nunjucks',
    mapping: {
      '.html': 'nunjucks'
    }
  };

  // 数据库配置
  exports.sequelize = {
    dialect: 'mysql', // support: mysql, mariadb, postgres, mssql
    database: 'mmcai',
    host: 'rm-bp130gbzu15s7pwacgo.mysql.rds.aliyuncs.com',
    port: '3306',
    username: 'jqtx',
    password: 'Ny123$%^',
  };

  // socket.io 配置
  exports.io = {
    namespace: {
      '/': {
        connectionMiddleware: ['connect'],
        packetMiddleware: []
      },
      '/chat': {
        connectionMiddleware: ['connect'],
        packetMiddleware: []
      }
    },
    onlineUsers:{}
  };


  return config;
};
